package com.example.content_android.me;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.content_android.R;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

public class RankingActivity extends AppCompatActivity {
    ListView listView;
    SimpleAdapter simpleAdapter;
    public List<Map<String, Object>> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏界面默认标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        //设置顶部状态栏黑底白字（最上面这一行显示电池、时钟、wifi等图标是状态栏）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.BLACK);
        }
        setContentView(R.layout.activity_ranking);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            initData();
        } catch (IOException e) {
            e.printStackTrace();
        }
        listView = this.findViewById(R.id.ranking_view);


        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.ranking_item, new String[]{"ranking_item_view_order","ranking_item_view_author","ranking_item_view_count"}, new int[]{R.id.ranking_item_view_order,R.id.ranking_item_view_author,R.id.ranking_item_view_count});
        listView.setAdapter(simpleAdapter);

    }
    private void initData() throws IOException {
        SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);

        String token = sp.getString("token","");
        Response response = OkHttpUtils
                .get()//
                .url("http://169.254.89.184:8888/fzulab/userClockInCount/getAll")//
                .addParams("token",token)
                .tag(this)//
                .build()//
                .execute();
        System.out.println(response);

        JSONObject jsonObject = JSONObject.parseObject(response.body().string());

        if (jsonObject.getInteger("code") == 500) {
            String msg = jsonObject.getString("msg");

            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
            return;

        }
        if (jsonObject.getInteger("code") == 0) {
            System.out.println("aaaaa");
            JSONArray result = jsonObject.getJSONArray("result");



            if (result != null && result.size() > 0) {
                for (int i = 0; i < result.size(); i++) {
                    JSONObject str = JSON.parseObject(result.get(i).toString());
                    String order= String.valueOf((i+1));
                    Map map = new HashMap();
                    map.put("ranking_item_view_order", order);
                    map.put("ranking_item_view_author", str.get("username").toString());
                    map.put("ranking_item_view_count", str.get("clockInCount").toString());
                    list.add(map);
                }
            }

        }

    }
    private List<Map<String, Object>> getData() {

        return list;
    }
}
